"""
# Copyright 2024 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License. 
"""


import torch
from amct_pytorch.nn.module.quantization.quant_calibration_op import QuantCalibrationOp


class Model(torch.nn.Module):
    def __init__(self, layer_idx):
        super().__init__()
        self.layer_idx = layer_idx
        
        self.k_proj = torch.nn.Linear(4, 4, bias=False)
        self.v_proj = torch.nn.Linear(4, 4, bias=False)
        
        # define QuantCalibrationOp layer
        self.quant_calibration_op = QuantCalibrationOp('./outputs/record.txt', {'act_algo': 'ifmr'})
        
    def forward(self, inputs):
        key_states = self.k_proj(inputs)
        key_states = key_states * 2
        # insert QuantCalibrationOp where you want to quantize
        op_name = 'model.{}.k_proj'.format(self.layer_idx) # op name cannot be repeated, in LlamaAttention can use layer_idx
        self.quant_calibration_op(op_name, key_states)
        value_states = self.v_proj(inputs)
        
        return key_states, value_states


if __name__ == '__main__':
    model = Model(layer_idx=1)
    model = model.eval()
    input_data = torch.randn((4, 4))
    with torch.no_grad():
        output = model(input_data)